package com.qingfeng.client.service;

import com.qingfeng.base.exception.LsException;
import com.qingfeng.base.util.CodeEnum;
import com.qingfeng.client.bean.AfterSell;
import com.qingfeng.client.bean.OrderGoods;
import com.qingfeng.client.bean.Trade;
import com.qingfeng.client.dao.AfterSellMapper;
import com.qingfeng.client.dao.OrderGoodsMapper;
import com.qingfeng.client.dao.OrderStoreMapper;
import com.qingfeng.client.dao.TradeMapper;
import com.qingfeng.client.util.RefundEnum;
import com.qingfeng.client.util.TradeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class AfterSellService {

    @Resource
    private AfterSellMapper afterSellDao;


    @Autowired
    private WxPayService wxPayService;

    @Resource
    private OrderGoodsMapper orderGoodsDao;

    @Resource
    private OrderStoreMapper orderStoreDao;

    @Resource
    private TradeMapper tradeDao;

    /**
     * 同意退款(@TODO 退款默认不修改库存，如果需要修改需自行添加）
     * 1.获取退款信息
     * 2。修改该商品交易为7售后完成
     * 2。订单中只有一商品且为该笔退款则直接修改订单状态为6关闭交易
     * 3。如果订单中有其他商品且为退款完成，修改该订单状态为6关闭交易
     * @param code 退款编码
     * @return
     */
    @Transactional
    public String agreeRefund(String code){
        //获取退款信息并且退款，修改售后状态
        AfterSell afterSell = afterSellDao.selectOneByCode(code);
        if (afterSell==null){
            return "该笔退款不存在";
        }
        if (!afterSell.getRefundStat().equals(RefundEnum.SELL_AGREE.getCode())){
            return "该商品未申请售后/退款";
        }

        OrderGoods orderGoods = orderGoodsDao.selectById(afterSell.getOrderGoodsId());
        orderGoods.setStat(TradeEnum.TRADE_REFUND_SUCCESS.getCode());
        orderGoodsDao.updateById(orderGoods);
        afterSell.setRefundStat(RefundEnum.WAIT_BUYER_AGREE.getCode());
        afterSell.setUpdateTime((int) (System.currentTimeMillis()/1000));

        //微信支付退款
        Trade trade = tradeDao.selectById(orderGoods.getTradeId());

        boolean flag = wxPayService.refund(trade.getPaycode(), code, trade.getPayment()+"", afterSell.getBackMoney()+"", afterSell.getDescribes());
        if (!flag) {
            throw new LsException(CodeEnum.ERROR.getCode(),"支付退款失败，请联系管理员");
        }

        afterSell.setFinishTime((int) (System.currentTimeMillis()/1000));
        afterSellDao.updateById(afterSell);
        //单个订单修改完成，修改orderGoods和orderStore 状态
        List<OrderGoods> orderGoodsList = orderGoodsDao.selectByOrderStoreId(orderGoods.getOrderStoreId(),afterSell.getStoreId());
        boolean updateOrderStoreFlag = orderGoodsList.stream().allMatch(orderGoods1 -> orderGoods1.getStat().equals(TradeEnum.TRADE_REFUND_SUCCESS.getCode()));
        if (updateOrderStoreFlag){
            orderStoreDao.updateStatById(TradeEnum.TRADE_CLOSED.getCode(),orderGoods.getOrderStoreId());
        }
        return null;
    }

    /**
     * 不同意退款，
     * 修改afterSell 退款状态，修改orderGoods状态
     * @param code
     * @return
     */
    public String disagree(String code){
        AfterSell afterSell = afterSellDao.selectOneByCode(code);
        if (afterSell==null){
            return "该笔退款不存在";
        }
        OrderGoods orderGoods = orderGoodsDao.selectById(afterSell.getOrderGoodsId());
        if (!afterSell.getRefundStat().equals(RefundEnum.SELL_AGREE.getCode())){
            return "该商品未申请售后/退款";
        }
        orderGoods.setStat(TradeEnum.TRADE_REFUND_FAIL.getCode());
        orderGoodsDao.updateById(orderGoods);
        afterSell.setRefundStat(RefundEnum.DISAGREE.getCode());
        afterSell.setUpdateTime((int) (System.currentTimeMillis()/1000));
        afterSellDao.updateById(afterSell);
        return null;
    }
}
